﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Xml;

namespace for7raid.vkPlayer
{
    public class EditViewModel : UIElement
    {
        private Window view;
        public EditViewModel(MediaItem item)
        {
            Item = item;
            item.UpdateMP3TagAsync();
            BindCommands();
        }


        public MediaItem Item { get; set; }

        private void BindCommands()
        {
            CommandBinding cb = new CommandBinding(ApplicationCommands.Save);
            cb.Executed += (s, e) => { Save(); };
            cb.CanExecute += new CanExecuteRoutedEventHandler(SaveCanExecute);
            this.CommandBindings.Add(cb);
        }

        protected virtual void SaveCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = User.Me != null && Item.Owner == User.Me.uid;
        }

        protected virtual void Save()
        {
            try
            {
                Item.Save();
                
                view.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show("Не могу сохранить запись");
            }
        }

        public Window BindToView(Window w)
        {
            w.DataContext = this;
            view = w;
            return w;
        }
    }
}
